<template>
  <div class="app-container">
    <eHeader :query="query"/>
    <!--表格渲染-->
    <el-table v-loading="loading" :data="data" size="small" style="width: 100%;">
      <el-table-column :show-overflow-tooltip="true" prop="orderCode" label="工单编号"/>
      <el-table-column prop="status" label="工单状态">
        <template slot-scope="scope">
          <div v-for="item in statusOptions" :key="item.id">
            <el-tag v-show="scope.row.status.toString() === item.value">{{ item.label }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="orderSource" label="工单类型">
        <template slot-scope="scope">
          <div v-for="item in orderSourceOptions" :key="item.key">
            <span v-show="scope.row.orderSource.toString() === item.key">{{ item.display_name }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="报修内容">
        <template slot-scope="scope">
          <span v-if="scope.row.orderSource===1">{{scope.row.taccident.faultDesc}}</span>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="报修时间">
        <template slot-scope="scope">
          <span v-if="scope.row.orderSource===1">{{ parseTime(scope.row.taccident.inputTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="任务内容">
        <template slot-scope="scope">
          <span v-if="scope.row.orderSource===2">{{scope.row.task.taskContent}}</span>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="发起时间">
        <template slot-scope="scope">
          <span v-if="scope.row.orderSource===2">{{ parseTime(scope.row.task.inputTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="巡检对象">
        <template slot-scope="scope">
          <span v-if="scope.row.orderSource===3">{{scope.row.inspection.objName}}</span>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="巡检时间">
        <template slot-scope="scope">
          <span v-if="scope.row.orderSource===3">{{ parseTime(scope.row.inspection.operateTime) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="serviceLevel" label="服务等级">
        <template slot-scope="scope">
          <div v-for="item in serviceLevels" :key="item.id">
            <el-tag v-show="scope.row.serviceLevel.toString() === item.value" >{{ item.label }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="faultType" label="故障类型">
        <template slot-scope="scope">
          <div v-for="item in faultTypes" :key="item.id">
            <el-tag v-show="scope.row.faultType.toString() === item.value" >{{ item.label }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="supplier" label="服务商"/>
      <el-table-column prop="assignOperator" label="指派人"/>
      <el-table-column :show-overflow-tooltip="true" prop="assignTime" label="指派时间">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.assignTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="acceptOperator" label="接单人"/>
      <el-table-column :show-overflow-tooltip="true" prop="acceptTime" label="接单时间">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.acceptTime) }}</span>
        </template>
      </el-table-column> -->
      <el-table-column v-if="checkPermission(['ADMIN','TWORKORDER_ALL','TWORKORDER_EDIT','TWORKORDER_DELETE'])" label="操作" width="150px" align="center">
        <template slot-scope="scope">
          <edit v-permission="['ADMIN','TTASKS_ALL','TTASKS_EDIT']" :data="scope.row" :sup_this="sup_this"/>
          <!-- <assign-order v-show="scope.row.status === 100" v-permission="['ADMIN','TWORKORDER_ALL','TWORKORDER_EDIT']" :data="scope.row" :sup_this="sup_this"/> -->
          <!-- <accept-order v-show="scope.row.status === 200" v-permission="['ADMIN','TWORKORDER_ALL','TWORKORDER_EDIT']" :data="scope.row" :sup_this="sup_this"/> -->
          <el-button v-show="scope.row.status === 201" v-permission="['ADMIN','TWORKORDER_ALL','TWORKORDER_EDIT']" style="margin-left: -2px" type="text" size="mini" @click="accept(scope.row.id)">拒单</el-button>
          <log-order v-permission="['ADMIN','TWORKORDER_ALL','TWORKORDER_EDIT']" :value="scope.row" :sup_this="sup_this"/>
          <suggest-order v-show="scope.row.status > 200"  v-permission="['ADMIN','TWORKORDER_ALL','TWORKORDER_EDIT']" :data="scope.row" :sup_this="sup_this"/>
          <el-popover
            v-permission="['ADMIN','TWORKORDER_ALL','TWORKORDER_DELETE']"
            :ref="scope.row.id"
            placement="top"
            width="180">
            <p>确定删除本条数据吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
              <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定</el-button>
            </div>
            <el-button slot="reference" type="text" size="mini">删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <el-pagination
      :total="total"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"/>
  </div>
</template>

<script>
import request from '@/utils/request'
import { get } from '@/api/dictDetail'
import checkPermission from '@/utils/permission'
import initData from '@/mixins/initData'
import initDict from '@/mixins/initDict'
import { del } from '@/api/tWorkOrder'
import { parseTime } from '@/utils/index'
import eHeader from './module/header'
import edit from './module/edit'
import AssignOrder from './module/components/assignOrder';
import AcceptOrder from './module/components/acceptOrder';
import LogOrder from './module/components/logOrder';
import SuggestOrder from './module/components/suggestOrder';
export default {
  components: { eHeader, edit,AssignOrder,AcceptOrder,LogOrder,SuggestOrder },
  mixins: [initData],
  data() {
    return {
      delLoading: false, sup_this: this,
      statusOptions: [],
      serviceLevels:[],
      faultTypes:[],
      orderSourceOptions: [
        { key: '1', display_name: '故障单' },
        { key: '2', display_name: '任务单' },
        { key: '3', display_name: '巡检单' }
      ]
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
      this.getDict("order_status")
      this.getDict("fault_type")
      this.getDict("service_level")
    })
  },
  methods: {
    parseTime,
    checkPermission,
    beforeInit() {
      this.url = 'api/tWorkOrder'
      const sort = 'id,desc'
      this.params = { page: this.page, size: this.size, sort: sort }
      const query = this.query
      //const type = query.type
      //const value = query.value
      //if (type && value) { this.params[type] = value }
      if (query.orderCode) {
        this.params["orderCode"] = query.orderCode;
      }
      if (query.status) {
        this.params["status"] = query.status;
      }
      return true
    },
    async getDict(name) {
      return new Promise((resolve, reject) => {
        get(name).then(res => {
          if(name=='order_status')
            this.statusOptions = res.content
          else if(name=='fault_type')
            this.faultTypes = res.content
          else 
            this.serviceLevels = res.content
          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    },
    subDelete(id) {
      this.delLoading = true
      del(id).then(res => {
        this.delLoading = false
        this.$refs[id].doClose()
        this.init()
        this.$notify({
          title: '删除成功',
          type: 'success',
          duration: 2500
        })
      }).catch(err => {
        this.delLoading = false
        this.$refs[id].doClose()
        console.log(err.response.data.message)
      })
    }
  }
}
</script>

<style scoped>
</style>
